//
//  HPCustomerListViewController.m
//  HappyPenguin
//
//  Created by zhuangyihang on 1/8/16.
//  Copyright © 2016 zhuangyihang. All rights reserved.
//

#import "HPCustomerListViewController.h"
#import "HPCustomerListCell.h"
#import "HPCustomerViewController.h"
#import "HPHttpVipQueryRequest.h"

@interface HPCustomerListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

//数据
@property (nonatomic, strong) NSArray *customers;
@property (nonatomic, strong) HPHttpVipQueryRequest *request;

@end

@implementation HPCustomerListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addBackButton:nil];
    [self setBaseNavigationBarTitle:@"会员列表"];
    
    [self setupUI];
    
    WS(ws);
    self.request = [[HPHttpVipQueryRequest alloc] init];
    self.request.type = VipQueryType_All;
    self.request.order = VipQueryOrder_TotalMoney;
    self.request.responseBlock = ^(HPHttpBasePagedResponse *response, HttpError *error){
        if (error) {
            
        }else{
            ws.customers = response.results;
            [ws.tableView reloadData];
            if (response.hasMore) {
                ws.tableView.showsInfiniteScrolling = YES;
            }else{
                ws.tableView.showsInfiniteScrolling = NO;
            }
        }
        [ws.tableView.infiniteScrollingView stopAnimating];
        [ws.tableView.pullToRefreshView stopAnimating];
    };
    [self.request pullRefresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Private
- (void)setupUI{
    WS(ws);
    self.tableView = [UITableView new];
    [self.baseContentView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.baseContentView);
        make.center.equalTo(self.baseContentView);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView addInfiniteScrollingWithActionHandler:^{
        [ws.request loadMore];
    }];
    
    [self.tableView addPullToRefreshWithActionHandler:^{
        [ws.request pullRefresh];
    }];
    
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.customers.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"HPCustomerCell";
    HPCustomerListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[HPCustomerListCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    
    VipQueryObject *obj = self.customers[indexPath.row];
    cell.textLabel.text = obj.vipName;
    cell.detailTextLabel.text = obj.infoString;
    
    [cell.portraitImageView sd_setImageWithURL:[NSURL URLWithString:@"https://pic4.zhimg.com/d5aed1527_l.jpg"]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [HPCustomerListCell getCellHeight];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    VipQueryObject *obj = self.customers[indexPath.row];
    HPCustomerViewController *v = [[HPCustomerViewController alloc] init];
    v.vipId = obj.vipId;
    [self.navigationController pushViewController:v animated:YES];
}

@end
